package com.rephilo.zk.controller;

import com.rephilo.zk.DataSourceUtils;
import com.rephilo.zk.service.ZookeeperListener;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class TestController {

    @Autowired
    private ZookeeperListener zookeeperListener;

    @GetMapping("/getDatasource")
    public Object getDatasource() {
        HikariDataSource dataSource = DataSourceUtils.getDataSource();
        if (dataSource != null) {
            return dataSource.getJdbcUrl();
        }
        return "no datasource";
    }

    @PostMapping("/changeDatasource")
    public Object changeUrl(@RequestParam String url) throws Exception {
        zookeeperListener.setData(url);
        return "ok";
    }
}
